package jp.co.cos.ittrick.channel.controller.client;

import java.util.Date;

import jp.co.cos.ittrick.channel.model.PageModel;
import jp.co.cos.ittrick.channel.service.PageService;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;

public class PublicPageController extends Controller {

    private PageService pageService;

    public PublicPageController() {
        pageService = new PageService();
    }

    @Override
    public Navigation run() throws Exception {

        // リクエストパラメータ
        String page = asString("page");

        PageModel pageModel = pageService.getPageByKey(Integer.valueOf(page));
        if(pageModel.getPublicFlg() != null && pageModel.getPublicFlg()) {
            pageModel.setPublicFlg(false);
        }else {
            pageModel.setPublicFlg(true);
            pageModel.setPublicDate(new Date());
        }

        pageService.update(pageModel);

        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html");
        response.getWriter().println("0");
        response.flushBuffer();

        return null;
    }
}
